#include "TreeNode.h"

#include "InterCode.h"

#include "syntax.tab.h"

#include "Mips.h"

//#define DEBUG //use gcc -D DEBUG can do
#ifdef DEBUG
yydebug = 1;
#endif

struct TreeNode* root=NULL;//the only definition, in Program will be changed as !0
extern int syntax_error;
extern int semantic_error;

void usage()
{
    const char *usage = "./parser [C--_TESTFILE.cmm] [INTERMEDIATE_REPRESENTATION_OUTPUTFILE.ir]\n";
    fprintf(stderr, "USAGE: %s", usage);
}

int main(int argc,char *argv[])
{
    if(argc<=1){
        printf("too few arguments!\n");
        usage();
        return -1;
    }
    else if(argc>3){
        printf("too many arguments!\n");
        usage();
        return -1;
    }
    FILE *fp = fopen(argv[1],"r");
    if(!fp)
    {
        perror(argv[1]);
        return -2;
    }
    yyrestart(fp);
    yyparse();
    //drawTree(root,0);
    
    init();
    Program(root);
    
    if(syntax_error == 0 && semantic_error == 0)
    {
        _Program(root);
        //优化部分
		computeConst();
        optimizeGoto();
		deleteLabel();

		printCode(argv[2]);
    }
    
    //could add a free_tree operation here

    return 0;
}
